#include "GEnemy.h"
LPDIRECT3DTEXTURE9 CEnemy::_enemyImage;
CEnemy::CEnemy(int left,int top,CScreen *sc,
				QuadTree *q,int kind,SOUND *sManager)
{
	_direction=Direction::Letf;
	vX=ENEMY_NORMAL_SPEED;
	lastTime=GetTickCount();
	_typeObj=2;
	_layer=2;
	_kind=kind;
	_quadTree=q;
	_pRECT=new RECT();
	_pRECT->left=left;
	_pRECT->top=top;
	_isActive=false;
	_isUpDown=false;
	_screen=sc;
	_hidden=-1;
	_status=0;
	_id=countObj++;
	_pSoundManager=sManager;
	bool r=(rand()%2==0?false:true);
	switch(_kind)
	{
	case 0://Goombas
		_pRECT->right=left+GOOMBAS_W;
		_pRECT->bottom=top+GOOMBAS_H;
		_pSprite=new CSprites(_enemyImage, true,0,2,0,GOOMBAS_W,GOOMBAS_H,left,_pRECT->bottom,0,160);
		break;
	case 1://Turtle green
		_pRECT->right=left+TURTLE_M_W;
		_pRECT->bottom=top+TURTLE_M_H;
		_pSprite=new CSprites(_enemyImage, true,0,2,(_direction==Direction::Right?GOOMBAS_H:(GOOMBAS_H+TURTLE_M_H)),TURTLE_M_W,TURTLE_M_H,left,_pRECT->bottom,0,160);
		break;
	case 2://Turtle red
		_pRECT->right=left+TURTLE_M_W;
		_pRECT->bottom=top+TURTLE_M_H;
		_pSprite=new CSprites(_enemyImage, true,0,2,(_direction==Direction::Right?(GOOMBAS_H+TURTLE_M_H*2):(GOOMBAS_H+TURTLE_M_H*3)),TURTLE_M_W,TURTLE_M_H,left,_pRECT->bottom,0,160);
		break;
	case 3://Turtle green have wing
		_pRECT->right=left+TURTLE_M_W;
		_pRECT->bottom=top+TURTLE_M_H;
		_pSprite=new CSprites(_enemyImage, true,0,2,(_direction==Direction::Right?(GOOMBAS_H*3+TURTLE_M_H*4):(GOOMBAS_H*3+TURTLE_M_H*5)),TURTLE_M_W,TURTLE_M_H,left,_pRECT->bottom,0,160);
		break;
	case 4://Turtle red have wing
		_pRECT->right=left+TURTLE_M_W;
		_pRECT->bottom=top+TURTLE_M_H;
		_pSprite=new CSprites(_enemyImage, true,0,2,(_direction==Direction::Right?(GOOMBAS_H+TURTLE_M_H*2):(GOOMBAS_H+TURTLE_M_H*3)),TURTLE_M_W,TURTLE_M_H,left,_pRECT->bottom,0,160);
		break;
	}
}

void CEnemy::Draw(int VirtualLeft,int Style)
{
	_pSprite->Draw(VirtualLeft,0,true);
}
int CEnemy::Move()
{
	if(GetTickCount()-lastTime<DELTA_TIME)
		return 0;
	lastTime=GetTickCount();
	int DeltaX=0;
	if(_isActive&&_status!=2)//'s not died yet.
	{
		DeltaX=CheckLeftRight();
		_pRECT->left+=DeltaX;
		_pRECT->right+=DeltaX;
		_pSprite->_left+=DeltaX;
		if(DeltaX==0)
			ChangeDirectory();
		if((_status==0||_status==-1)&&!_quadTree->IsOnObject(_pRECT,_id)&&_kind!=3)
		{
			_status=1;//Jump
			_isUpDown = true ; 
			_deltaY = 0;
			isUp=false;
		}
		if (_kind==3)
		{
		
		}

	}
	Jump();
	Die();
	return DeltaX;
}
void CEnemy::ChangeDirectory()
{
	if(_direction==Direction::Letf)
		_direction=Direction::Right;
	else
		_direction=Direction::Letf;
	if(_kind!=0&&_status!=-1&&_status!=-2)
		if(_direction==Direction::Letf)
		{
			_pSprite->_row+=_pSprite->_height;
		}
		else
		{
			_pSprite->_row-=_pSprite->_height;
		}
}
void CEnemy::ChangeStatus(int style)//bi dap hay bi ban
{
	switch(_status)
	{
	case -2://Turtle stand
		_pSprite->_width=TURTLE_D_W;
		_pSprite->_height=TURTLE_D_H;
		_pSprite->_index=0;
		_pSprite->_count=0;
		if (_kind==1||_kind==3)
			_pSprite->_row=GOOMBAS_H+TURTLE_M_H*4;
		if (_kind==2)
			_pSprite->_row=GOOMBAS_H*2+TURTLE_M_H*4;
		_pSprite->_interval=0;
		break;
	case -1://Turtle move fast.
		_pSprite->_width=TURTLE_D_W;
		_pSprite->_height=TURTLE_D_H;	
		_pSprite->_index=0;
		_pSprite->_count=3;
		if (_kind==1||_kind==3)
			_pSprite->_row=GOOMBAS_H+TURTLE_M_H*4;
		if (_kind==2)
			_pSprite->_row=GOOMBAS_H*2+TURTLE_M_H*4;;
	
		_pSprite->_interval=100;
		break;
	case 2://die
		if(_kind==0)//Goombas	
		{
			if(style==0)//to step
			{
				_pSprite->_interval=0;
				_pSprite->_index=3;
				_pSprite->_rootTime=GetTickCount();
				_pSprite->_totalExistTime=4000;//Time to remove
			}
			else 
				if(style==1)//to fire
				{
					_pSprite->_interval=0;
					_pSprite->_index=2;
					_isUpDown = true ; //up down before die
					_deltaY = 15;
					isUp=true;
				}
			_isActive=false;
		}
		if(_kind==1||_kind==2||_kind==3)//Turtle
		{
			if(style==1)//to fire
			{
				_pSprite->_width=TURTLE_D_W;
				_pSprite->_height=TURTLE_D_H;
				_pSprite->_row=GOOMBAS_H+TURTLE_M_H*4;
				_pSprite->_interval=0;
				_pSprite->_index=1;
				_pSprite->_count=3;
				if (_kind==1||_kind==3)
					_pSprite->_row=GOOMBAS_H+TURTLE_M_H*4;
				if (_kind==2)
					_pSprite->_row=GOOMBAS_H*2+TURTLE_M_H*4;
				_isUpDown = true ; //up down before die
				_deltaY = 15;
				isUp=true;
			}
		}
		break;
	}
}

void CEnemy::Collision(Object *Ob)
{
	if(Ob->_typeObj==1)//mario
	{
		if(Ob->_status==1 && !((CEnemy*)Ob)->isUp)
		{
			Ob->_pRECT->top-=30;
			Ob->_pRECT->bottom-=30;
			Ob->_pSprite->_top-=30;
			if(_kind==0)
				_status=2;
			if(_kind==1||_kind==2)//Turtle
			{
				switch(_status)
				{
				case -2:
					vX=ENEMY_HIGH_SPEED;
					_status=-1; break;
				case -1:
					vX=0;
					_status=-2; break;
				case 0:
					_pSprite->_top+=16;
					_pRECT->top+=16;
					vX=0;
					_status=-2;	break;					
				}
			}
			if (_kind==3)
			{
				_status=2;
				ChangeStatus(1);
			}
			ChangeStatus(0);//to step
			_pSoundManager->Play("EnemyDie",false);
			info->CointCounter+=2;
		}
		else
		{
			Ob->ObjGetColliton(-1);
		}

	}
	if((Ob->_status==-1&&Ob->_typeObj==2)||Ob->_typeObj==4)//collection with turtle shells or bullet
	{
		if(Ob->_typeObj==4)
		{
			Ob->_status=2;
		}
		_status=2;
		ChangeStatus(1);
	}
}
CEnemy::~CEnemy(void)
{
}

void CEnemy::Jump()
{
	if(_isUpDown&&_status==1)
	{
		if(isUp)
		{
			_deltaY--;
			_deltaY=CheckUpDown(true);
			_pSprite->_top-=_deltaY;
			_pRECT->top-=_deltaY;
			_pRECT->bottom-=_deltaY;
		}
		else
		{
			if(_deltaY<10)
				_deltaY++;
			_deltaY=CheckUpDown(false);
			_pSprite->_top+=_deltaY;
			_pRECT->top+=_deltaY;
			_pRECT->bottom+=_deltaY;
			if(_pSprite->_top>=WINDOW_HEIGHT||_deltaY==0)
			{
				if(_deltaY==0)
				{
					_status=0;
					_isUpDown=false;
				}
				else
				{
					_status=2;
				}
				return;
			}
		}
		if(_deltaY<=0)
		{
			isUp=false;
		}
	}
}

void CEnemy::Die()
{
	if(_isUpDown&&_status==2)//Died
	{
		if(isUp)
		{
			_deltaY--;
			_pSprite->_top-=_deltaY;
			_pRECT->top-=_deltaY;
			_pRECT->bottom-=_deltaY;
		}
		else
		{
			_deltaY++;
			_pSprite->_top+=_deltaY;
			_pRECT->top+=_deltaY;
			_pRECT->bottom+=_deltaY;
			if(_pSprite->_top>=WINDOW_HEIGHT)
			{
				_isUpDown=false;
				return;
			}
		}
		if(_deltaY<=0)
			isUp=false;
	}

}
int CEnemy::CheckLeftRight()
{
	float x;
	
	x=DELTA_TIME*vX;
	if(x==0)
		return 0;
	x=x*_direction;
	RECT r;
	r.left=_pRECT->left+x;
	r.right=_pRECT->right+x;
	r.top=_pRECT->top;
	r.bottom=_pRECT->bottom;
	Object *ob=NULL;
	Object *o=NULL;
	while((o=_quadTree->CheckCollision(_quadTree->_pRoot,r,_id))!=NULL
		||(_screen!=NULL&&(r.left<_screen->_limLeft||r.right>_screen->_limRight)))//neu dung do voi cac object khac
	{
		if(o!=NULL)
		{
			ob=o;						
			if((o->_typeObj==3&&_typeObj==1)||(o->_typeObj==3&&_typeObj==2)||(o->_typeObj==2&&_typeObj==3)
				||(o->_typeObj==2&&_typeObj==2&&_status==-1)||(_typeObj==2&&ob->_typeObj==1)||(_typeObj==3&&ob->_typeObj==3)
				||(o->_typeObj==3&&_typeObj==4)||(o->_typeObj==4&&_typeObj==3))
				break;
		}
		x-=_direction;
		r.left-=_direction;
		r.right-=_direction;
		if(x==0)
		{
			break;
		}
	}
	if(ob!=NULL&&ob->_typeObj!=0)
		ob->Collision(this);
	if(x!=0)
		_justMove=true;
	return x;
}
int CEnemy::CheckUpDown(bool IsUp)
{
	int y=(_isUpDown?_deltaY:0);
	int z=y=(IsUp?-y:y);
	if(_isUpDown)
	{
		RECT r;
		r.left=_pRECT->left;
		r.right=_pRECT->right;
		r.top=_pRECT->top+y;
		r.bottom=_pRECT->bottom+y;
		Object *ob=NULL;
		Object *o=NULL;
		while((o=_quadTree->CheckCollision(_quadTree->_pRoot,r,_id))!=NULL)//check collition with other obj
		{
			if(o!=NULL)
			{
				ob=o;				
				if((o->_typeObj==3&&_typeObj==1)||(o->_typeObj==3&&_typeObj==2)||(o->_typeObj==2&&_typeObj==3)
					||(_typeObj==1&&ob->_typeObj==2&&isUp)||(o->_typeObj==3&&_typeObj==4)||(o->_typeObj==4&&_typeObj==3))
					break;
			}
			if(!IsUp)
			{
				y--;
				r.top--;
				r.bottom--;
			}
			else
			{
				y++;
				r.top++;
				r.bottom++;
			}
			if(y==0)
			{						
				break;
			}
		}
		if(ob!=NULL)
		{
			if(ob->_typeObj==0)
			{
				if(isUp)
					ob->Collision(this);
			}
			else
				ob->Collision(this);
		}
	}
	if(y!=0)
		_justMove=true;
	return (y<0?-y:y);
}

void CEnemy::EnemyFly(int limTop, int limBottom)
{
	int a=rand()%4;
	if (_pSprite->_top>300)
		a=-a;	
	if (_pSprite->_top<50)
		a=-a;
	_pRECT->top+=a;
	_pRECT->bottom+=a;
	_pSprite->_top+=a;

}